//
//  ByViewDayEarningQuery.m
//  mijifen_ios
//
//  Created by mc on 2017/10/27.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewDayEarningQuery.h"

@implementation ByViewDayEarningQuery
{
//    UIView      *_heaederBgView;
    UIButton    *_queryButton;
    UIView      *_lineView;
    UILabel     *_yearLabel;
    UILabel     *_monthLabel;
    UIButton    *_yearUpButton;
    UIButton    *_yearDownButton;
    UIButton    *_monthUpButton;
    UIButton    *_monthDownButton;
    NSInteger    _yearSelectTag;
    NSInteger    _monthSelectTag;
}


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self createViews];
    }
    return self;
}
- (void)createViews{
    
    CGFloat width = [@"查询" getSizeOfString:16].width + 60;
    _queryButton = [[UIButton alloc] init];
    [self addSubview:_queryButton];
    [_queryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(10);
        make.right.equalTo(self.mas_right).offset(-10);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    [_queryButton setTitle:@"查询" forState:UIControlStateNormal];
    [_queryButton setTitleColor:BY_TEXT_COLOR forState:UIControlStateNormal];
    _queryButton.layer.cornerRadius = 30 / 2;
    _queryButton.layer.borderWidth = 1;
    _queryButton.layer.borderColor = BY_TEXT_COLOR.CGColor;
    [_queryButton addTarget:self action:@selector(queryAction) forControlEvents:UIControlEventTouchUpInside];
    
    // 线
    CGFloat line_x = (SCREEN_WIDTH - width - 10) / 2;
    _lineView = [[UIView alloc] init];
    [self addSubview:_lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(15);
        make.left.equalTo(self.mas_left).offset(line_x);
        make.size.mas_equalTo(CGSizeMake(1, 20));
    }];
    
    // 年(箭头 30:15)
    width = [@"2020" getSizeOfString:16].width + 15;
    CGFloat year_margin = (line_x - width - 30) / 2;
    _yearLabel = [[UILabel alloc] init];
    [self addSubview:_yearLabel];
    [_yearLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(year_margin);
        make.top.equalTo(self.mas_top).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    _yearLabel.font = FONT(16);
    _yearLabel.textColor = [UIColor darkGrayColor];
    
    _yearUpButton = [[UIButton alloc] init];
    [self addSubview:_yearUpButton];
    [_yearUpButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(7.5);
        make.left.equalTo(_yearLabel.mas_right);
        make.size.mas_equalTo(CGSizeMake(30, 15));
    }];
    [_yearUpButton setImage:[UIImage imageNamed:@"Index_arrow_up"] forState:UIControlStateNormal];
    [_yearUpButton setImage:[UIImage imageNamed:@"Index_arrow_upSlected"] forState:UIControlStateSelected];
    
    _yearDownButton = [[UIButton alloc] init];
    [self addSubview:_yearDownButton];
    [_yearDownButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_yearUpButton.mas_bottom).offset(5);
        make.left.equalTo(_yearUpButton.mas_left);
        make.size.mas_equalTo(CGSizeMake(30, 15));
    }];
    [_yearDownButton setImage:[UIImage imageNamed:@"Index_arrow_down"] forState:UIControlStateNormal];
    [_yearDownButton setImage:[UIImage imageNamed:@"Index_arrow_downSelected"] forState:UIControlStateSelected];
    
    // 月
    width = [@"12" getSizeOfString:16].width + 10;
    CGFloat month_margin = (line_x - width - 30) / 2;
    _monthLabel = [[UILabel alloc] init];
    [self addSubview:_monthLabel];
    [_monthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lineView.mas_right).offset(month_margin);
        make.top.equalTo(self.mas_top).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 30));
    }];
    _monthLabel.font = FONT(16);
    _monthLabel.textColor = [UIColor darkGrayColor];
    
    _monthUpButton = [[UIButton alloc] init];
    [self addSubview:_monthUpButton];
    [_monthUpButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_monthLabel.mas_right);
        make.top.equalTo(_yearUpButton.mas_top);
        make.size.mas_equalTo(CGSizeMake(30, 15));
    }];
    [_monthUpButton setImage:[UIImage imageNamed:@"Index_arrow_up"] forState:UIControlStateNormal];
    [_monthUpButton setImage:[UIImage imageNamed:@"Index_arrow_upSlected"] forState:UIControlStateSelected];
    
    _monthDownButton = [[UIButton alloc] init];
    [self addSubview:_monthDownButton];
    [_monthDownButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_monthUpButton.mas_left);
        make.top.equalTo(_monthUpButton.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(30, 15));
    }];
    [_monthDownButton setImage:[UIImage imageNamed:@"Index_arrow_down"] forState:UIControlStateNormal];
    [_monthDownButton setImage:[UIImage imageNamed:@"Index_arrow_downSelected"] forState:UIControlStateSelected];

    [_yearUpButton addTarget:self action:@selector(yearAction:) forControlEvents:UIControlEventTouchUpInside];
    [_yearDownButton addTarget:self action:@selector(yearAction:) forControlEvents:UIControlEventTouchUpInside];
    [_monthUpButton addTarget:self action:@selector(monthAction:) forControlEvents:UIControlEventTouchUpInside];
    [_monthDownButton addTarget:self action:@selector(monthAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _yearDownButton.selected = YES;
    _monthDownButton.selected = YES;
    _yearUpButton.tag = 10;
    _monthUpButton.tag = 12;
    _yearDownButton.tag = _yearSelectTag = 11;
    _monthDownButton.tag = _monthSelectTag = 13;


    // 测试数据
    _yearLabel.text = @"2017";
    _monthLabel.text = @"10";
    
}

// 点击事件
- (void)yearAction:(UIButton *)button{
    if (_yearSelectTag == button.tag) {
        if (button.tag == 10) {
            _yearLabel.text = [NSString stringWithFormat:@"%ld",_yearLabel.text.integerValue - 1];
        }else{
            _yearLabel.text = [NSString stringWithFormat:@"%ld",_yearLabel.text.integerValue + 1];
        }
        
    }else{
        UIButton *preButton = [self viewWithTag:_yearSelectTag];
        preButton.selected = NO;
        
        button.selected = YES;
        if (button.tag == 10) {
             _yearLabel.text = [NSString stringWithFormat:@"%ld",_yearLabel.text.integerValue - 1];
        }else{
             _yearLabel.text = [NSString stringWithFormat:@"%ld",_yearLabel.text.integerValue + 1];
        }
        _yearSelectTag = button.tag;

    }
}

- (void)monthAction:(UIButton *)button{
    if (_monthSelectTag == button.tag) {
        if (button.tag == 12) {
            _monthLabel.text = [NSString stringWithFormat:@"%ld",_monthLabel.text.integerValue <= 1 ? 1:_monthLabel.text.integerValue - 1];
        }else{
            _monthLabel.text = [NSString stringWithFormat:@"%ld",_monthLabel.text.integerValue >= 12 ? 12:_monthLabel.text.integerValue + 1];
        }
    }else{
        UIButton *preButton = [self viewWithTag:_monthSelectTag];
        preButton.selected = NO;
        
        button.selected = YES;
        if (button.tag == 12) {
            _monthLabel.text = [NSString stringWithFormat:@"%ld",_monthLabel.text.integerValue <= 1 ? 1:_monthLabel.text.integerValue - 1];
        }else{
            _monthLabel.text = [NSString stringWithFormat:@"%ld",_monthLabel.text.integerValue >= 12 ? 12:_monthLabel.text.integerValue + 1];
        }
        _monthSelectTag = button.tag;
    }
}

// 查询
- (void)queryAction{
    if (self.queryBlock) {
        self.queryBlock(_yearLabel.text, _monthLabel.text);
    }
}


@end
